<template>
  <!-- 对话框 -->
  <!-- 新增部门的弹层 -->
  <!-- :model  :rules  prop  v-model -->
  <el-dialog
    :title="dialogTitle"
    :visible="isShow"
    @close="handleCloseDialog"
    @open="getSimpleUserList"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <!-- label显示给用户看的 value将来提交给后台的 -->
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetSimpleUserList } from '@/api/user'
import { reqAddDepartment, reqGetDepartDetail, reqUpdateDepartDetail } from '@/api/departments'

export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    deptList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      // 根据老父亲id, 可以找到所有兄弟
      // 除了父级部门的id, 还需要原始数据(部门列表),
      console.log(value, this.nodeData.id, this.deptList)

      let arr = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        arr = this.deptList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.deptList.filter(item => item.pid === this.nodeData.id)
      }

      const isRepeat = arr.some(item => item.name === value)
      isRepeat ? callback(new Error('同级部门已经出现该部门名称了')) : callback()
    }
    const validateCode = (rule, value, callback) => {
      if (this.form.id && value === this.nodeData.code) {
        // 编辑情况下, code可以和原来的一样, 通过校验
        callback()
        return
      }
      // 部门编码不能在所有部门中重复 => item.code
      const isRepeat = this.deptList.some(item => item.code === value)
      isRepeat ? callback(new Error('部门编码已经重复了')) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门名称只能是1-10位', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门编码只能是1-10位', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍只能是1-300位', trigger: ['blur', 'change'] }
        ]
      },
      userList: []

    }
  },
  computed: {
    dialogTitle() {
      return this.form.id ? '编辑部门' : '添加子部门'
    }
  },
  created() {
    this.getSimpleUserList()
  },
  methods: {
    handleCloseDialog() {
      console.log('关闭')
      // 通知父组件 修改数据
      this.$emit('update:isShow', false)
      this.$refs.form.resetFields()

      // 手动重置
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async getSimpleUserList() {
      const { data } = await reqGetSimpleUserList()
      //   console.log(res, 6868)
      this.userList = data
    },
    clickSubmit() {
      this.$refs.form.validate(async(flag) => {
        if (!flag) return
        let res

        if (this.form.id) {
          // 编辑
          res = await reqUpdateDepartDetail(this.form)
          console.log(res)
        } else {
          // 添加
          res = await reqAddDepartment({
            ...this.form,
            pid: this.nodeData.id
          })
          // console.log(res)
        }
        // 提示
        this.$message.success(res.message)
        // 关闭
        this.handleCloseDialog()
        // 重新渲染
        this.$emit('add-dept')
      })
    },
    async getDepartDetail() {
      const { data } = await reqGetDepartDetail(this.nodeData.id)
      // console.log(res, 5555)
      this.form = data
    }
  }
}
</script>

<style>

</style>
